package com.xuexiang.xuidemo.util;

import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

/**
 * 存储模块的工具类
 *
 * @since 2021-04-22
 */
public class SettingSPUtils {
    private static volatile SettingSPUtils sInstance = null;
    private Preferences preferences;

    private SettingSPUtils(Context context) {
        DatabaseHelper databaseHelper = new DatabaseHelper(context);
        String fileName = "xui";
        preferences = databaseHelper.getPreferences(fileName);
    }

    /**
     * 获取单例
     *
     * @param context context
     * @return SettingSPUtils单例对象
     */
    public static SettingSPUtils getInstance(Context context) {
        if (sInstance == null) {
            synchronized (SettingSPUtils.class) {
                if (sInstance == null) {
                    sInstance = new SettingSPUtils(context);
                }
            }
        }
        return sInstance;
    }


    private final String IS_FIRST_OPEN_KEY = "is_first_open_key";

    private final String IS_AGREE_PRIVACY_KEY = "is_agree_privacy_key";

    private final String IS_USE_CUSTOM_THEME_KEY = "is_use_custom_theme_key";

    private final String IS_USE_CUSTOM_FONT_KEY = "is_use_custom_font_key";

    /**
     * 是否是第一次启动
     *
     * @return boolean
     */
    public boolean isFirstOpen() {
        return preferences.getBoolean(IS_FIRST_OPEN_KEY, true);
    }

    /**
     * 设置是否是第一次启动
     *
     * @param isFirstOpen isFirstOpen
     */
    public void setIsFirstOpen(boolean isFirstOpen) {
        preferences.putBoolean(IS_FIRST_OPEN_KEY, isFirstOpen);
        preferences.flush();
    }

    /**
     * isAgreePrivacy
     *
     * @return 是否同意隐私政策
     */
    public boolean isAgreePrivacy() {
        return preferences.getBoolean(IS_AGREE_PRIVACY_KEY, false);
    }

    /**
     * 设置布尔属性值
     *
     * @param isAgreePrivacy
     */
    public void setIsAgreePrivacy(boolean isAgreePrivacy) {
        preferences.putBoolean(IS_AGREE_PRIVACY_KEY, isAgreePrivacy);
        preferences.flush();
    }

    /**
     * 获取布尔状态值
     *
     * @return boolean
     */
    public boolean isUseCustomTheme() {
        return preferences.getBoolean(IS_USE_CUSTOM_THEME_KEY, false);
    }

    /**
     * 设置布尔类型用户自定义主题
     *
     * @param isUseCustomTheme
     */
    public void setIsUseCustomTheme(boolean isUseCustomTheme) {
        preferences.putBoolean(IS_USE_CUSTOM_THEME_KEY, isUseCustomTheme);
        preferences.flush();
    }

    /**
     * 获取是否自定义字体布尔值
     *
     * @return boolean
     */
    public boolean isUseCustomFont() {
        return preferences.getBoolean(IS_USE_CUSTOM_FONT_KEY, false);
    }

    /**
     * 设置自定义字体的布尔值
     *
     * @param isUseCustomFont
     */
    public void setIsUseCustomFont(boolean isUseCustomFont) {
        preferences.putBoolean(IS_USE_CUSTOM_FONT_KEY, isUseCustomFont);
        preferences.flush();
    }

}
